<template>
  <el-dialog
    v-model="model"
    title="添加原料信息"
    width="800px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div class="cntr">
      <el-form
        ref="form"
        :model="editFormFields"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="原料编号" prop="RawmanagementBh">
              <el-input v-model="editFormFields.RawmanagementBh"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原料名称" prop="RawmanagementName">
              <el-input v-model="editFormFields.RawmanagementName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类" prop="FlName">
              <el-select v-model="editFormFields.FlName" placeholder="请选择">
                <el-option
                  v-for="item in flNameOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号" prop="XinghaoName">
              <el-select v-model="editFormFields.XinghaoName" placeholder="请选择">
                <el-option
                  v-for="item in xinghaoOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      model: false,
      editFormFields: {
        RawmanagementBh: "",
        RawmanagementName: "",
        FlName: "",
        XinghaoName: "",
      },
      rules: {
        RawmanagementBh: [
          { required: true, message: "请输入原料编号", trigger: "blur" },
        ],
        RawmanagementName: [
          { required: true, message: "请输入原料名称", trigger: "blur" },
        ],
        FlName: [
          { required: true, message: "请选择分类", trigger: "change" },
        ],
        XinghaoName: [
          { required: true, message: "请选择型号", trigger: "change" },
        ],
      },
      flNameOptions: [],
      xinghaoOptions: [],
    };
  },
  methods: {
    open() {
      this.model = true;
      this.getOptions();
    },
    handleClose() {
      this.model = false;
      this.$refs.form.resetFields();
    },
    getOptions() {
      // 获取分类选项
      this.http.get("api/Raw/getFlNameOptions").then((res) => {
        this.flNameOptions = res.map(item => ({
          value: item.value,
          label: item.label
        }));
      });
      
      // 获取型号选项
      this.http.get("api/Raw/getXinghaoOptions").then((res) => {
        this.xinghaoOptions = res.map(item => ({
          value: item.value,
          label: item.label
        }));
      });
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.http
            .post("api/Raw/addRaw", this.editFormFields)
            .then((res) => {
              if (res.success) {
                this.$Message.success("保存成功");
                this.$parent.setTable1Rows("Add", [res.data]);
                this.handleClose();
              } else {
                this.$Message.error(res.message);
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.cntr {
  padding: 15px 20px;
}
.dialog-footer {
  text-align: right;
  padding-right: 20px;
}
</style> 